package com.aysafety.iotdataplatformserver.listener;

import com.alibaba.fastjson.JSON;
import com.aysafety.core.DBDriver.hbase.HBaseDriver;
import com.aysafety.core.common.DateUtils;
import com.aysafety.core.model.ElectromagneticIntensityModel;
import com.aysafety.iotdataplatformserver.dao.ElectromagneticIntensityDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;

// 电磁强度
@Component
public class ElectromagneticIntensityListener {

    private final String type = "ElectromagneticIntensity";

    private final String tableName = "ElectromagneticIntensityRealTime";

    private final String calTableName = "CalElectromagneticIntensity";

    private final String[] calStructure = new String[]{"warningIndicator", "deviation", "deviationContinued"};

    private final String[] structure = new String[]{"ElectromagneticIntensity"};

    private final String calWarningTableName = "CalWarningElectromagneticIntensity";

    private final String[] calWarningStructure = new String[]{"warning"};

    private Logger logger = LoggerFactory.getLogger(ErrorListener.class);

    @Autowired
    KsTreeDataListener ksTreeDataListener;

    @Autowired
    ElectromagneticIntensityDao electromagneticIntensityDao;

    public boolean createTable() throws IOException {
        return HBaseDriver.createTable(this.tableName, this.structure);
    }

    public boolean createCalTable() throws IOException {
        return HBaseDriver.createTable(this.calTableName, this.calStructure);
    }

    public boolean createCalWarningTable() throws IOException {
        return HBaseDriver.createTable(this.calWarningTableName, this.calWarningStructure);
    }


    public void start(Map<String, Object> result) {
        try {
            String clientId = result.get("clientId").toString();
            if (!result.get("dataType").toString().equals("histroy")) {
                List<Map<String, Object>> listDatas = JSON.parseObject(result.get("data").toString(), List.class);
                for (Map<String, Object> listData : listDatas) {
                    ElectromagneticIntensityModel electromagneticIntensity = this.transformData(listData);
                    this.save(electromagneticIntensity, clientId);
                }
            } else {
                List<Map<String, Object>> listDatas = JSON.parseObject(result.get("data").toString(), List.class);
                for (Map<String, Object> listData : listDatas) {
                    ElectromagneticIntensityModel electromagneticIntensity = this.transformData(listData);
                    this.save(electromagneticIntensity, clientId);
                }
            }
        } catch (IOException e) {
            logger.info("ElectromagneticIntensityListener:", e);
        }
    }

    public boolean save(ElectromagneticIntensityModel electromagneticIntensity, String clientId) throws IOException {
        String timestamp = String.valueOf(electromagneticIntensity.getDatetime().getTime());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("tableId", electromagneticIntensity.getEntId() + "." + "ElectromagneticIntensityRealTime");
        map.put("pk", "\'" + timestamp +".:." + electromagneticIntensity.getDeviceId()+ "\'");
        map.put("clientId", "\'" + clientId + "\'");
        map.put("deviceId", "\'" + electromagneticIntensity.getDeviceId() + "\'");
        map.put("entId", "\'" + electromagneticIntensity.getEntId() + "\'");
        map.put("unit", "\'" + electromagneticIntensity.getUnit() + "\'");
        map.put("workingFace", "\'" + electromagneticIntensity.getWorkingFace() + "\'");
        map.put("datetime", electromagneticIntensity.getDatetime().getTime());
        map.put("monitoringValue", electromagneticIntensity.getMonitoringValue());
        map.put("poklad", electromagneticIntensity.getPoklad());
        map.put("smallestRange", electromagneticIntensity.getSmallestRange());
        map.put("maximumRange", electromagneticIntensity.getMaximumRange());
        map.put("stationName", "\'" + electromagneticIntensity.getStationName() + "\'");
        map.put("status", electromagneticIntensity.isStatus());
        map.put("powerCutArea", "\'" + electromagneticIntensity.getPowerCutArea() + "\'");
        map.put("powerReconnectionLimit", electromagneticIntensity.getPowerReconnectionLimit());
        map.put("powerCutLimit", electromagneticIntensity.getPowerCutLimit());
        electromagneticIntensityDao.insertElectromagneticIntensityRealTime(map);
        ksTreeDataListener.generateTreeData(electromagneticIntensity.getEntId(), this.type,
            electromagneticIntensity.getWorkingFace(), electromagneticIntensity.getDeviceId(),
            electromagneticIntensity.getDatetime(), "");
        return true;
    }

    public ElectromagneticIntensityModel transformData(Map<String, Object> result) {
        ElectromagneticIntensityModel electromagneticIntensity = new ElectromagneticIntensityModel();
        if (null != result.get("entId")) {
            electromagneticIntensity.setEntId(result.get("entId").toString());
        } else {
            electromagneticIntensity.setEntId("无数据");
        }
        if (null != result.get("deviceId")) {
            electromagneticIntensity.setDeviceId(result.get("deviceId").toString());
        } else {
            electromagneticIntensity.setDeviceId("无数据");
        }
        electromagneticIntensity.setMonitoringValue(Double.valueOf(result.get("monitoringValue").toString()));
        if (null != result.get("poklad")) {
            electromagneticIntensity.setPoklad(Double.valueOf(result.get("poklad").toString()));
        } else {
            electromagneticIntensity.setPoklad(-1.0);
        }
        electromagneticIntensity.setDatetime(DateUtils.fomatDateTime(Long.valueOf(result.get("datetime").toString())));
        if (null != result.get("workingFace")) {
            electromagneticIntensity.setWorkingFace(result.get("workingFace").toString());
        } else {
            electromagneticIntensity.setWorkingFace("无数据");
        }
        if (null != result.get("unit")) {
            electromagneticIntensity.setUnit(result.get("unit").toString());
        } else {
            electromagneticIntensity.setUnit("无数据");
        }
        if (null != result.get("smallestRange")) {
            electromagneticIntensity.setSmallestRange(Double.valueOf(result.get("smallestRange").toString()));
        } else {
            electromagneticIntensity.setSmallestRange(-1.0);
        }
        if (null != result.get("maximumRange")) {
            electromagneticIntensity.setMaximumRange(Double.valueOf(result.get("maximumRange").toString()));
        } else {
            electromagneticIntensity.setSmallestRange(-1.0);
        }
        if (null != result.get("stationName")) {
            electromagneticIntensity.setStationName(result.get("stationName").toString());
        } else {
            electromagneticIntensity.setUnit("无数据");
        }
        if (null != result.get("status")) {
            if (result.get("status").toString().equals("1")) {
                electromagneticIntensity.setStatus(TRUE);
            } else {
                electromagneticIntensity.setStatus(FALSE);
            }
        } else {
            electromagneticIntensity.setStatus(FALSE);
        }
        if (null != result.get("powerCutArea")) {
            electromagneticIntensity.setPowerCutArea(result.get("powerCutArea").toString());
        } else {
            electromagneticIntensity.setPowerCutArea("无数据");
        }
        if (null != result.get("powerReconnectionLimit")) {
            electromagneticIntensity
                    .setPowerReconnectionLimit(Double.valueOf(result.get("powerReconnectionLimit").toString()));
        } else {
            electromagneticIntensity.setPowerReconnectionLimit(-1.0);
        }
        if (null != result.get("powerCutLimit")) {
            electromagneticIntensity.setPowerCutLimit(Double.valueOf(result.get("powerCutLimit").toString()));
        } else {
            electromagneticIntensity.setPowerCutLimit(-1.0);
        }
        return electromagneticIntensity;
    }
}
